package com.bankapp.service.bank;

import com.bankapp.domain.bank.Email;

import java.util.LinkedList;
import java.util.List;

public class EmailQueue {
    private List<Email> queue = new LinkedList<Email>();

    public void add(Email email) {
        synchronized (queue) {
            queue.add(email);
            queue.notifyAll();
        }
    }

    public boolean isEmpty() {
        synchronized (queue) {
            return queue.isEmpty();
        }
    }

    public Email getEmail() {
        synchronized (queue) {
            while (queue.isEmpty()) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    return null;
                }
            }
            return queue.remove(0);
        }
    }
}